<?php

/**
 * 腾讯im
 * @author hexuanlin
 * @version  $Revision 1.0  2020-09-29 12:28
 */

namespace app\helper;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class Excel
{
    private $handle;
    public function __construct()
    {
        $this->handle = new Spreadsheet();
    }
    /**
     * 这是表格属性
     *
     * @param array $props
     * @return void
     */
    public function setProp($props = []){
        $this->handle->getProperties()
            ->setCreator('小奇迹科技')
            ->setLastModifiedBy("小奇迹科技")
            ->setTitle($props['title'] ?? '报表')
            ->setSubject($props['title'] ?? '报表')
            ->setDescription($props['desc'] ?? ($props['desc'] ?? '报表'))
            ->setKeywords($props['title'] ?? '报表')
            ->setCategory("Test result file");
    }
    // 给表格添加数据
    public function setData($data = []){
        $sheet = $this->handle->getActiveSheet();
        foreach($data as $k => $row){
            foreach(array_values($row) as $j => $cell){
                $key = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V'][$j] . ($k + 1);
                $sheet->setCellValue($key, $cell);
            }
        }
    }
    // 
    public function save($file){        
        $writer = new Xlsx($this->handle);
        $writer->save(root_path() . 'runtime/excel/' . $file . '.xlsx');
    }
}
